package com.xbx.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbx.eduservice.entity.EduSubject;
import com.xbx.eduservice.entity.EduTeacher;
import com.xbx.eduservice.entity.excel.SubjectData;
import com.xbx.eduservice.entity.subject.OneSubject;
import com.xbx.eduservice.entity.subject.TwoSubject;
import com.xbx.eduservice.listener.SubjectExcelListener;
import com.xbx.eduservice.mapper.EduSubjectMapper;
import com.xbx.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.security.auth.Subject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-22
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Autowired
    public EduSubjectService eduSubjectService;
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            InputStream inputStream= file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getOneTwoSubject() {
        List<OneSubject> result=new ArrayList<>();
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        //取出所有parentid为0的对象，结果为onelist集合
        wrapper.eq("parent_id","0");
        List<EduSubject> onelist=eduSubjectService.list(wrapper);
        //遍历onelist集合
        for(EduSubject eduSubject:onelist){
            OneSubject oneSubject=new OneSubject();
            List<TwoSubject> children=new ArrayList<>();
            //给一级分类添加id和title
            oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());
            QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
            //根据一级分类，查询出对应的二级分类，将其放置于twolist中
            wrapper2.eq("parent_id",eduSubject.getId());
            List<EduSubject> twolist= eduSubjectService.list(wrapper2);
            for(EduSubject eduSubject2:twolist){
                TwoSubject twoSubject=new TwoSubject();
                twoSubject.setId(eduSubject2.getId());
                twoSubject.setTitle(eduSubject2.getTitle());
                children.add(twoSubject);
            }
            oneSubject.setChildren(children);
            result.add(oneSubject);
        }
        return result;
    }
}
